package com.higgins.client.knowledge;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.higgins.client.projectmanagement.ProjectService;
import com.higgins.client.projectmanagement.ProjectServiceAsync;

public class Knowledge implements EntryPoint {
	private final KnowledgeServiceAsync knowledgeService = GWT
												.create(KnowledgeService.class);
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void onModuleLoad() {
		authenticate();
	}
	private void executeKnowledge()	{
		final String level = Window.Location.getParameter("level");
		TabPanel tabPanel = new TabPanel();
		KnowledgeTree tree = new KnowledgeTree(level);
		tree.initialise();
		KnowledgeColumns columns = new KnowledgeColumns();
		KnowledgeTable table = new KnowledgeTable(level, columns);
		tree.addTreeListener(table);
//		table.addRefreshable(tree);
		DockLayoutPanel tasksPanel = new DockLayoutPanel(Unit.EM);
		ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setHeight("600px");
		scrollPanel.add(tree);
		tasksPanel.addWest(scrollPanel, 20);
		tasksPanel.add(table);
		
		tabPanel.add(tasksPanel, "Knowledge");
		tabPanel.selectTab(0);
		
		RootLayoutPanel.get().add(tabPanel);
		tabPanel.setSize("200em","100em");
		tasksPanel.setSize("200em","100em");
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>()    {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				int selection = event.getSelectedItem();
				
				switch (selection)	{
				case 0:
					break;
				}
			}
		});
					
	}
	
	private void authenticate()	{
		knowledgeService.checkAccess(new AsyncCallback<Boolean>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Boolean result) {
				executeKnowledge();
			}
		});
	}
}
